//
//  PLPlatform.h
//  Plankton
//
//  Created by Marc Schlichte on 2008-07-12.
//  Copyright 2008 Framework Labs. All rights reserved.
//

#import <Cocoa/Cocoa.h>

#import "PLEndpoint.h"


@class PLPattern;

@protocol PLMessageHandler <NSObject>

- (void)handleMessage:(id)message forPattern:(PLPattern *)pattern;

@end

@protocol PLTimeoutHandler <NSObject>

- (void)handleTimeoutForPattern:(PLPattern *)pattern;

@end

@interface PLTargetActionPair : NSObject
{
    id  target;
    SEL action;
}
@end

@interface PLTargetActionMessageHandler : PLTargetActionPair <PLMessageHandler>

+ (PLTargetActionMessageHandler *)handlerWithTarget:(id)target action:(SEL)action;

@end

@interface PLTargetActionTimeoutHandler : PLTargetActionPair <PLTimeoutHandler>

+ (PLTargetActionTimeoutHandler *)handlerWithTarget:(id)target action:(SEL)action;

@end

@interface PLPattern : NSObject
{
    NSString                *receiver;
    id<PLMessageHandler>    messageHandler;
    id<PLTimeoutHandler>    timeoutHandler;
    NSTimeInterval          timeoutInterval;
    NSTimer                 *timeoutTimer;
    id                      userInfo;
}

+ (PLPattern *)patternAny;
+ (PLPattern *)patternWithPredicate:(NSPredicate *)predicate;
+ (PLPattern *)patternWithPredicateFormat:(NSString *)predicateFormat, ...;

- (PLPattern *)setMessageHandler:(id<PLMessageHandler>)handler;
- (PLPattern *)setMessageTarget:(id)target action:(SEL)action;

- (PLPattern *)setTimeoutHandler:(id<PLTimeoutHandler>)timeout after:(NSTimeInterval)interval;
- (PLPattern *)setTimeoutTarget:(id)target action:(SEL)action after:(NSTimeInterval)interval;

@property(retain) id userInfo;

- (BOOL)matchesContent:(id)message;

@end

@interface PLPlatform : NSObject <PLEndpointDelegate>
{
    PLEndpoint      *endpoint;
    NSUInteger      nextLocalAid;
    NSMutableArray  *patterns;
    NSMutableArray  *messages;
    unsigned long long nextTag; 
}

- (NSString *)createAid;
+ (int)portOfAid:(NSString *)aid;

- (void)send:(id)message to:(NSString *)aid;
- (void)multicast:(id)message to:(NSSet *)aids;

- (void)receive:(PLPattern *)pattern by:(NSString *)aid;

- (NSNumber *)createTag;

@end
